package com.yzr.util;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description 文件类型工具类
 * @author: yzr
 * @date: 2021/10/29 15:00
 **/
public final class FileType {

    private static final List<String> OFFICE = new ArrayList<>();
    private static final List<String> IMAGE = new ArrayList<>();
    private static final List<String> TEXT = new ArrayList<>();
    private static final String PDF = "pdf";
    private static final String SVG = "svg";

    static {
        Collections.addAll(TEXT, "txt", "csv", "html", "xml", "js", "sql",
                "property", "java", "yml", "bat", "log", "c", "json");

        Collections.addAll(OFFICE, "xls", "xlsx", "doc", "docx", "ppt", "pptx");
        Collections.addAll(IMAGE, "png", "gif", "jpeg", "bmp");
    }

    public static Support attribution(File file) throws FileNotFoundException {
        if (!FileUtil.isFile(file)) {
            throw new FileNotFoundException("文件不存在");
        }
        final String type = FileTypeUtil.getType(file);

        if (OFFICE.contains(type)) {
            return Support.OFFICE;
        } else if (IMAGE.contains(type)) {
            return Support.IMAGE;
        } else if (TEXT.contains(type)) {
            return Support.TEXT;
        } else if (PDF.equals(type)) {
            return Support.PDF;
        } else if (SVG.equals(type)) {
            return Support.SVG;
        } else {
            return Support.OTHER;
        }
    }

    /**
     * 获取文件的归类
     *
     * @param filePath 文件地址
     * @return 枚举类型
     * @throws FileNotFoundException 文件不存在异常
     */
    public static Support attribution(String filePath) throws FileNotFoundException {
        return attribution(FileUtil.file(filePath));
    }



    public static boolean isOffice(String fileName) {
        return OFFICE.contains(FileOfType.getTypeByName(fileName));
    }

    public static boolean isImage(String fileName) {
        return IMAGE.contains(FileOfType.getTypeByName(fileName));
    }

    public static boolean isText(String fileName) {
        return TEXT.contains(FileOfType.getTypeByName(fileName));
    }

    public static boolean isPdf(String fileName) {
        return PDF.equals(FileOfType.getTypeByName(fileName));
    }

    public static boolean isSvg(String fileName) {
        return SVG.equals(FileOfType.getTypeByName(fileName));
    }

    public static String getPDF() {
        return PDF;
    }

    public static String getSvg() {
        return SVG;
    }

    public static List<String> getOFFICE() {
        return OFFICE;
    }

    public static List<String> getIMAGE() {
        return IMAGE;
    }

    public static List<String> getTEXT() {
        return TEXT;
    }

    public enum Support {
        OFFICE,
        IMAGE,
        TEXT,
        PDF,
        TIF,
        SVG,
        OTHER;
    }
}
